package utilities;


import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;

import com.jgoodies.forms.layout.Size;

import model.SalesReportItems;
import model.CustomerReportItems;
import model.Stock;

import dbhandeling.Connections;
import dbhandeling.Crude;

public class ViewUtilites {
	
	public static final Connections dbe=new Connections();
	
	
	
	
	// takes string and gives java.sql.date
	
	public static java.sql.Date convert (String temp)
	{
		 SimpleDateFormat sdf = null;
		 System.out.println(temp);
		if(temp.contains("-"))
		   sdf=new SimpleDateFormat("yyyy-MM-dd");
		if(temp.contains("/"))
			sdf=new SimpleDateFormat("MM/dd/yyyy");
		java.util.Date date = null;
		
		try {
			
			date = sdf.parse(temp);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		java.sql.Date dates = new java.sql.Date(date.getTime());
		return dates;
	}
	
	
	// takes list and string aray and sets the list
	public static void setList(JList list, String[] data)
	{
		DefaultListModel dl=(DefaultListModel) list.getModel();
		for(int i=0;i<data.length;i++)
		{
			dl.addElement(data[i]);
		}
	}
	
	
	// takes table and returns the corresponding data as a 2d aray
	
	public static String[][] ret_data(JTable tab)
	{		DefaultTableModel dm=(DefaultTableModel) tab.getModel();
	        int rowcount=dm.getRowCount();
	        int colcount=dm.getColumnCount();
	        String[][] rd=new String[rowcount][colcount];
	        for(int i=0;i<rowcount;i++)	        	
	        {
	        	for(int j=0;j<colcount;j++)
	        	{
	        		rd[i][j]= dm.getValueAt(i,j)+"";
	        	}
	        }
		return rd;
	}
	
// sets the combo box;

public static void set_comboBox(JComboBox com,String[]items)
{
	int itemCount = com.getItemCount();

    for(int i=itemCount-1;i>-1;i--)
    {
        com.removeItemAt(i);
    }
	
    for(int i=0;i<items.length;i++)
	{
		com.addItem(items[i]);
	}
}

// Methods takes a jlabel or jtext and sets the date funvtionality on them
public static void makeDateComponent(Object object)
{
	
	JLabel label;
	JTextField jtext;
	
	if(object instanceof JLabel)
	{
		label=(JLabel) object;
		SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy");
		Calendar  rightNow = Calendar.getInstance();
		DatePicker myDialog = new DatePicker(new JFrame(), true, rightNow);
		rightNow = myDialog.getAnswer();
		label.setText(sdf.format(rightNow.getTime()));
		myDialog.dispose();
	}
	
	else if(object instanceof JTextField )
	{
		jtext=(JTextField) object;;
		SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy");
		Calendar  rightNow = Calendar.getInstance();
		DatePicker myDialog = new DatePicker(new JFrame(), true, rightNow);
		rightNow = myDialog.getAnswer();
		jtext.setText(sdf.format(rightNow.getTime()));
		myDialog.dispose();
	}
	
	
	
	
}
public static void setTable(Object[] data,String[] columns,JTable table)
{
	Object temp=data[0];
	String className= temp.getClass().getName();
	Object[]row = new Object[columns.length];
	DefaultTableModel dm= (DefaultTableModel) table.getModel();
	
	try {
		HashMap<String, Method> hash=null;
		
		for(int i =0 ;i<data.length;i++)  // This will be the number of rows
		{
			temp =data[i]; // You have to get all the attribute value of temp maintaining the order specified
			                      // in columns
		   for(int k=0;k<row.length;k++)
		   {
			   Method m=hash.get(columns[k]);
			   row[k]=m.invoke(temp);
		   }
		   
		   dm.addRow(row);
		   
			}
		
		
		// Start reading the hash map
		
		
	           
		
		
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
}

public static void makeAutoCompleteJtextbox(JTextField textField,List<String>data,boolean strictMatching)
{
	AutoCompleteDecorator.decorate(textField,data, strictMatching);
}

public static Timestamp getTime()
{
	java.util.Date d=new java.util.Date();
	Timestamp t=new Timestamp(d.getTime());
	return t;
}

public static void setAutoSuggestionsComboBox(JComboBox x)
{
	AutoCompleteJComboBoxer auto=new AutoCompleteJComboBoxer(x);
}

public static void sendSMS()
{
	
}
public static void sendEmail()
{
	
}


public static void setTable(Stock[] stocks, String[] cols, JTable table) {
	// TODO Auto-generated method stub
	
}


public static void setTable(SalesReportItems[] data, String[] cols,
		JTable table) {
	// TODO Auto-generated method stub
	
}


public static void setTable(CustomerReportItems[] data, String[] cols,
		JTable table) {
	// TODO Auto-generated method stub
	
}
}
